package com.minsu.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.minsu.dto.ListingImportDTO;
import com.minsu.entity.Listing;
import com.minsu.service.ListingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ListingImportListener extends AnalysisEventListener<ListingImportDTO> {
    private final ListingService listingService;
    private final List<ListingImportDTO> list = new ArrayList<>();
    private final List<String> errorMessages = new ArrayList<>();
    private static final int BATCH_COUNT = 100;

    public ListingImportListener(ListingService listingService) {
        this.listingService = listingService;
    }

    @Override
    public void invoke(ListingImportDTO data, AnalysisContext context) {
        log.info("解析到一条数据:{}", data);
        // 数据校验
        if (!validateData(data)) {
            return;
        }
        list.add(data);
        if (list.size() >= BATCH_COUNT) {
            saveData();
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData();
        log.info("所有数据解析完成！");
    }

    private void saveData() {
        log.info("{}条数据，开始存储数据库！", list.size());
        List<Listing> listings = convertToEntities();
        listingService.saveBatch(listings);
        log.info("存储数据库成功！");
    }

    private List<Listing> convertToEntities() {
        List<Listing> listings = new ArrayList<>();
        for (ListingImportDTO dto : list) {
            Listing listing = new Listing();
            listing.setCreatedAt(LocalDateTime.now());
            listing.setUpdatedAt(LocalDateTime.now());
            listing.setTitle(dto.getTitle());
            listing.setPrice(BigDecimal.valueOf(dto.getPrice()));
            listing.setLocation(dto.getLocation());
            listing.setRoomType(dto.getRoomType());
            listing.setArea(BigDecimal.valueOf(dto.getArea()));
            listing.setHostName(dto.getHostName());
            listing.setHostResponseRate(dto.getHostResponseRate());
            listing.setIsSuperHost("是".equals(dto.getIsSuperHost()));
            listing.setDescription(dto.getDescription());
            listing.setStatus(1); // 默认上架状态
            listings.add(listing);
        }
        return listings;
    }

    private boolean validateData(ListingImportDTO data) {
        if (!StringUtils.hasText(data.getTitle())) {
            errorMessages.add("标题不能为空");
            return false;
        }
        if (data.getPrice() == null || data.getPrice() <= 0) {
            errorMessages.add("价格必须大于0");
            return false;
        }
        if (!StringUtils.hasText(data.getLocation())) {
            errorMessages.add("位置不能为空");
            return false;
        }
        if (data.getArea() == null || data.getArea() <= 0) {
            errorMessages.add("面积必须大于0");
            return false;
        }
        return true;
    }

    public List<String> getErrorMessages() {
        return errorMessages;
    }
} 